* Aggregate analyses

***** Risk
cd "$pathdata/Risk"
use Risk_cleaned, clear

keep if treatment == 0


replace ce=ce/100
replace lottery_probability = lottery_probability/100

gen delta=.
gen gamma=.
gen beta=.
gen default=.
gen alpha=.



// * Restricted Model
nl (switching_point= lottery_probability^(1/{alpha=1})*lottery_amount), cl(id)  
estat ic


// * With CU and CARA
nl (switching_point = (1-min(1,max(0,(1-{gamma=1}*cognitive_uncertainty)))) * {default=1}*lottery_amount + min(1,max(0,(1-{gamma=1}*cognitive_uncertainty))) * lottery_probability^(1/{alpha=1})*lottery_amount), cl(id)
estat ic



* Restrictions for plot
drop if ce/lottery_probability > 10 | ce/lottery_probability < 0.1


// * With CU no CARA
nl (switching_point = (1-min(1,max(0,(1-{gamma=1}*cognitive_uncertainty)))) * {default=1}*lottery_amount + min(1,max(0,(1-{gamma=1}*cognitive_uncertainty))) * lottery_probability*lottery_amount), cl(id)
predict pred_cu
estat ic
predict res, residuals
replace pred_cu = (pred_cu/lottery_amount)


// *Just collapsing CU by p
bysort lottery_probability: egen cu = mean(cognitive_uncertainty)
nl (switching_point = (1-min(1,max(0,(1-{gamma=1}*cu)))) * {default=1}*lottery_amount + min(1,max(0,(1-{gamma=1}*cu))) * lottery_probability^(1/{alpha=1})*lottery_amount), cl(id)
predict pred_cu_s
estat ic
replace pred_cu_s = (pred_cu_s/lottery_amount)



preserve

foreach i in pred_cu pred_cu_s lottery_probability ce {
	replace `i'=`i'*100
}

collapse (mean) pred_cu_s ce cognitive_uncertainty pred_cu (semean) se_ce=ce se_cu=pred_cu (count) no = ce, by(lottery_probability)

gen upper=ce+se_ce
gen lower=ce-se_ce
gen upper_cu=pred_cu+se_cu
gen lower_cu=pred_cu-se_cu



tw (scatter ce lottery_probability, msize(large)  ms(+) mcolor(black)) ///
	(line pred_cu_s lottery_probability, lwidth(medthick) lcolor(green%80))  ///
 	(function y=x , range(0 100) lpattern(dash) lcolor(black) lwidth(thin) ), ///
	ytitle("Normalized certainty equivalent") ///
    xtitle("Payoff probability") xscale( lcolor(none)) ysc(lcolor(none)) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	graphregion(color(white)) ///
	ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
	legend(order(1 2) label(1 "Subject Data") label(2 "Model fit") r(2))

restore
